import { Empty, Table } from 'antd'
import './index.scss'
import { DragSortTable } from '@ant-design/pro-components'
import classNames from 'classnames';
import noData from '../../assets/images/noData.png'
import dayjs from 'dayjs';
import { useEffect, useRef } from 'react';

export default function Sheet({ columns, dataSource, page, onChange, rowSelection, isDrag = false, rowKey = "ID", fileChane }) {
  const columnsData = columns.map(item => ({ ...item, ellipsis: true }))
  const wait = async (delay = 1000) =>
    new Promise((resolve) => setTimeout(() => resolve(void 0), delay));
  const actionRef = useRef()
  useEffect(() => {
    actionRef.current?.reload()
  }, [fileChane])
  return (
    <div className={classNames({ 'sheet-container': !isDrag })}>
      {isDrag ? <DragSortTable rowSelection={{
        ...rowSelection,
        fixed: true
      }} alwaysShowAlert={true} scroll={
        {
          x: true
        }
      } columns={columnsData} pagination={page ? {
        showSizeChanger: true,
        showQuickJumper: true,
        current: page.pageNum,
        pageSize: page.pageSize,
        showTotal: ((total) => `共计${total}条`),
        onChange: onChange
      } : false}
        search={false}
        actionRef={actionRef}
        request={async () => {
          const data = await dataSource();
          const _data = data.data.data.map(item => {
            // for (let key in item) {
            //   if (key === 'CREATE_ON' || key === 'REQ_REQUEST_DATE') {
            //     item[key] = item[key] ? dayjs(item[key]).format('YYYY-MM-DD') : null
            //   }
            // }
            return {
              ...item
            }
          })
          return {
            data: _data,
            total: data.data.total,
            success: true,
          };
        }}
        rowKey={rowKey}
        dragSortKey="sort"
        locale={{
          emptyText: <Empty image={noData}></Empty>
        }}
      /> : <Table loading={!Array.isArray(dataSource.data)} rowKey={rowKey} rowSelection={rowSelection && {
        ...rowSelection,
        fixed: true
      }} scroll={
        {
          x: true
        }
      } columns={columnsData} dataSource={dataSource.data} pagination={page ? {
        showSizeChanger: true,
        showQuickJumper: true,
        total: dataSource.total,
        current: page.pageNum,
        pageSize: page.pageSize,
        showTotal: ((total) => `共计${total}条`),
        onChange: onChange
      } : false}
        locale={{
          emptyText: <Empty image={noData}></Empty>
        }}
      ></Table>}
    </div>
  )
}
